import * as vscode from "vscode";
import { HarmonyPilotGUIWebviewViewProvider } from "../HarmonyPilotGUIWebviewViewProvider";
import { registerAllCommands } from "../commands";
import { VerticalDiffCodeLensProvider } from '../diff/vertical/VerticalPerLineCodeLensProvider';
import { VerticalDiffManager } from "../diff/vertical/manager";
import { LLMFactory } from "../../core/llm/llmFactory";
import { CommentCollectionFactory, CommentCollectionType } from "../../core/comments/CommentCollectionFactory";
import { MCPClient } from "../../core/mcp/mcpClient";
import { MCPClientAndProvider } from "../../core/mcp/mcpClientAndProvider";
import { StatusBar } from "./StatusBar";
import { LoginService } from "../services/loginService";
import { ProxyService } from "../services/proxyService";

const { registerCodeLensProvider } = vscode.languages;

export class VsCodeExtension {

    private verticalDiffManager: VerticalDiffManager;

    private sidebar: HarmonyPilotGUIWebviewViewProvider;

    private statusBar: StatusBar;

    private mcpClientAndProvider: MCPClientAndProvider;

    constructor(context: vscode.ExtensionContext) {
        this.verticalDiffManager = new VerticalDiffManager();
        let provider = new VerticalDiffCodeLensProvider(
            this.verticalDiffManager.fileUriToCodeLens,
        );
        registerCodeLensProvider(
            "*",
            provider,
        );
        this.verticalDiffManager.refreshCodeLens = provider.refresh.bind(provider);
        
        // 获取评论工厂
        const commentCollectionFactory = this.initCommentCollectionFactory(context);

        // 获取模型工厂
        const llmFactory = new LLMFactory();

        // 创建MCP客户端
        const mcpClient = new MCPClient();

        // 创建webview
        this.sidebar = new HarmonyPilotGUIWebviewViewProvider(context.extensionUri, context.extensionMode, context, mcpClient);

        // 创建MCP客户端提供者
        this.mcpClientAndProvider = new MCPClientAndProvider(context, mcpClient, this.sidebar);

        // 设置sidebar的mcpClientAndProvider引用
        this.sidebar.setMcpClientAndProvider(this.mcpClientAndProvider);

        // 注册所有命令
        registerAllCommands(context, this.verticalDiffManager, llmFactory, this.sidebar, commentCollectionFactory);
        // 注册侧边栏
        context.subscriptions.push(
            vscode.window.registerWebviewViewProvider(
            "HarmonyPilot.harmonyPilotGUIView",
            this.sidebar,
            {
                webviewOptions: { retainContextWhenHidden: true },
            },
            ),
        );

        // 创建登录状态栏
        this.statusBar = new StatusBar(context);
        this.statusBar.update();

        // 登录或者登出时执行的动作
        const loginService = LoginService.getInstance(context);
        context.subscriptions.push(
            loginService.onDidLoginStatusChange(() => {
                // 更新状态栏
                this.statusBar.update();
                // 更新webview
                this.sidebar.updateLoginStatus();
            })
        );

        ProxyService.getInstance(context).onAuthError(() => {
            loginService.fireLoginStatusChange();
        });
    }

    /**
     * 初始化评论工厂
     * @param context 
     * @returns 
     */
    private initCommentCollectionFactory(context: vscode.ExtensionContext): CommentCollectionFactory {
        const commentCollectionFactory = new CommentCollectionFactory(context);
        // Initialize comment collections for each type in CommentCollectionType enum
        for (const type in CommentCollectionType) {
            if (isNaN(Number(type))) {
                commentCollectionFactory.createCommentCollection(CommentCollectionType[type as keyof typeof CommentCollectionType]);
            }
        }
        return commentCollectionFactory;
    }
}